import axios from 'axios'
import qs from 'qs'
import { Message } from 'element-ui';

let baseUrl = ''
if (process.env.NODE_ENV === 'development') {
    // baseUrl = 'http://192.168.2.168:8095'
    //  baseUrl = 'http://210.21.48.72:8195'
   // baseUrl = 'http://192.168.2.104:8095';
    baseUrl = 'http://10.15.30.179:8195';
} else if (process.env.NODE_ENV === 'production') {
    // baseUrl = 'http://10.177.8.109:10002'
  
     baseUrl = 'http://10.177.8.109:10002';
}



// axios.defaults.baseURL = 'http://192.168.2.168:8081';;//发请求默认网址 //成海
axios.defaults.baseURL = baseUrl;;//发请求默认网址 //刘辉

// axios.defaults.baseURL = '/api';
axios.defaults.timeout = 30000
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
// axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'

//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
    //在发送请求之前做某件事
 
    const USER_TOKEN = JSON.parse(sessionStorage.getItem('usrObj'))?JSON.parse(sessionStorage.getItem('usrObj')).token:''
  
 
    if (USER_TOKEN) {
        config.headers.token = USER_TOKEN;
    }
     return config;
}, (error) => {
    console.log('64');
    return Promise.reject(error);
});

//返回状态判断(添加响应拦截器)
axios.interceptors.response.use((res) => {
    //对响应数据做些事

    if (res.status === 200) {
        if (res.data.code == '00' || res.data.code == 200) {
            return Promise.resolve(res);
        } else {
            Message({
                message: res.data.msg || res.data.message,
                type: 'error',
            })
        }
    } else {
        return Promise.reject(res)
    }
}, (error) => {
    // console.log(error)
    if (error.response) {
        switch (error.response.status) {
            case 400:
            case 401:
            case 403:
                if (error.response.data.message) {
                    Message({
                        message: error.response.data.message,
                        type: 'error',
                        duration: 3000,
                    })
                }
                break;
            case 404:
                Message({
                    message: '对象不存在！',
                    type: 'error',
                    duration: 5000,
                })
                break;
            case 500:
                Message({
                    message: '服务器发生了一些错误哦！',
                    type: 'error',
                    duration: 5000,
                })
                break;
            default:
                Message({
                    message: error.response.data.message,
                    type: 'error',
                    duration: 5000,
                })
                break;
        }
    } else if (error.code === 'ECONNABORTED' && error.message.indexOf('timeout') !== -1) {
        Message({
            message: '请求超时',
            type: 'error',
            duration: 5000,
        })
    }
    return Promise.reject(error);
});

//返回一个Promise(发送post请求)
export function fetchPost(url, params) {
    return new Promise((resolve, reject) => {
        axios.post(url, params)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
////返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: param
        })
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
////删除
export function fetchDelete(url, param) {
    return new Promise((resolve, reject) => {
        axios.delete(url + '?' + qs.stringify(param))
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
//// patch
export function fetchPatch(url, param = {}) {
    return new Promise((resolve, reject) => {
        axios.patch(url, param)
            .then(response => {
                resolve(response);
            }, err => {
                reject(err);
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export default {
    fetchPost,
    fetchGet,
    fetchDelete,
    fetchPatch,
}
